package net.chowda.castcluster.util;

import java.io.OutputStream;
import java.io.IOException;
import java.io.Writer;

/**
 * wrap writer with outputstream
 */
public class WriterOutputStream extends OutputStream {
    protected Writer w;
    private byte[] buf = new byte[1];

    public WriterOutputStream(Writer writer) {
        w = writer;
    }

    public void close() throws IOException {
        w.close();
        w = null;
    }

    public void flush() throws IOException {
        w.flush();
    }

    public void write(byte[] b) throws IOException {
        w.write(new String(b));
    }

    public void write(byte[] b, int off, int len) throws IOException {
        w.write(new String(b, off, len));
    }

    public synchronized void write(int b) throws IOException {
        buf[0] = (byte) b;
        write(buf);
    }
}